package cn.wildfire.chat.app.main;

import android.view.MotionEvent;
import android.view.View;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.List;

import cn.wildfire.chat.app.extendsion.ui.SmoothToggleAndSlideAnimationForUI;
import cn.wildfire.chat.app.extendsion.ui.TipDialog;
import cn.wildfire.chat.app.extendsion.utils.TakeoverInterfaceHub;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.group.BasePickGroupMemberActivity;
import cn.wildfire.chat.kit.group.manage.OnPickUserStatusChangeListener;
import cn.wildfirechat.model.UserInfo;

/**
 * Create by HEKE, 2019/8/23 20:25
 * TODO 接管转让群主功能界面
 */
public class TakeOverGroupOwerActivity extends BasePickGroupMemberActivity {

    private List<UIUserInfo> checkedGroupMembers;

    @Override
    protected void onGroupMemberChecked(List<UIUserInfo> checkedUserInfos) {
        this.checkedGroupMembers = checkedUserInfos;
    }

    @Override
    protected int menu() {
        return 0;
    }

    /**
     * 执行转让
     */
    private void turnOnwer() {
        try {
            if (checkedGroupMembers == null || checkedGroupMembers.size() == 0) {
                return;
            }
            UserInfo userInfo = checkedGroupMembers.get(0).getUserInfo();
            TipDialog.msgTip(this, getString(R.string.transfer_set), getString(R.string.set_takeover_group_tip, userInfo.displayName)
                    , new TipDialog.OnMsgTipDialogListener() {
                        @Override
                        public void then(boolean confirm,boolean outtouch) {
                            if (confirm) {
                                TakeoverGroupListener takeoverGroupListener = TakeoverInterfaceHub.getTakeoverGroupListener();
                                if (takeoverGroupListener != null) {
                                    takeoverGroupListener.callback(userInfo);
                                }
                                finish();
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * modify by HEKE , 2019/9/4 18:55 TODO 测试滑动隐藏或显示搜索栏 HEKE_SEARCHLAYOUT_ADDMANAGER
     */
    protected SmoothToggleAndSlideAnimationForUI smoothToggleAndSlideAnimationForUI;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        try {
            if (smoothToggleAndSlideAnimationForUI == null) {
                smoothToggleAndSlideAnimationForUI = new SmoothToggleAndSlideAnimationForUI(this,
                        SmoothToggleAndSlideAnimationForUI.FLAG_GROUP_ADD_REMOVE_MANMAGER);
                smoothToggleAndSlideAnimationForUI.setToggleView(pickGroupMemberFragment.getSearchLayout());
                smoothToggleAndSlideAnimationForUI.setSlideView(pickGroupMemberFragment.getUsersRecyclerView(), 0);
            }
            smoothToggleAndSlideAnimationForUI.onTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 2);
        setReplaceTitle(null, R.string.transfer_set);
        setSearchLayoutVisibility(false);
        setTextThenBinClickForRightTextView(View.VISIBLE, getString(R.string.confirm), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = getRightTxtView().getText().toString();
                if (text.equals(getString(R.string.confirm))) {
                    turnOnwer();
                } else {
                    // 隐藏搜索内容
                    pickGroupMemberFragment.hideSearchContactFragment();
                }
            }
        });
        pickGroupMemberFragment.setOnPickUserStatusChangeListener(onPickUserStatusChangeListener);
        pickGroupMemberFragment.setFromTurnGroupOnwer(true);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        /**
         * 转让群组只允许转让给一个人
         * */
        if (hasFocus) {
            if (pickGroupMemberFragment != null) {
                pickGroupMemberFragment.getPickUserViewModel().setMaxPickCount(1);
            }
        }
    }

    /**
     * 控制右上角文字变化，搜索显示与取消状态监听
     */
    private OnPickUserStatusChangeListener onPickUserStatusChangeListener = new OnPickUserStatusChangeListener() {
        @Override
        public void showSearchResult(boolean searching) {
            getRightTxtView().setText(searching ? getString(R.string.done_title) : getString(R.string.confirm));
        }
    };

}
